function resizePanes() {
	$('articlesPane').style.height = (document.body.clientHeight - 51) + "px";
	$('readingPane').style.height = (document.body.clientHeight - 81) + "px";
	$('readingPane').style.width = (document.body.clientWidth - 276) + "px";
	$('shortcutsPane').style.top = (document.body.clientHeight - 22) + "px";
	$('shortcutsPane').style.width = (document.body.clientWidth - 276) + "px";
}
function xTop(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curtop += obj.offsetTop;
		}
	}
	return curtop;
}
function scrollDn() {
	if (currentArtId >= numRows) {return;}
	var scrollBy = xTop($('art_' + currentArtId)) - xTop($('readingPane'));
	$('scrolling').style.top = ($('scrolling').offsetTop - scrollBy) + "px";
	$('articlesPane').scrollTop = 0;
	removeSelected();
	currentArtId++;
	loadSelected();
	return false;
}
function scrollUp() {
	if (currentArtId < 2) {return;}
	var scrollBy = xTop($('art_' + (currentArtId-1))) - xTop($('art_' + (currentArtId-2)));
	$('scrolling').style.top = ($('scrolling').offsetTop + scrollBy + 2) + "px";
	$('articlesPane').scrollTop = 0;
	removeSelected();
	currentArtId--;
	loadSelected();
	return false;
}
function openCurentLink() {
	action('open', 'add', $F('id_' + currentArtId));
}
var stopDefaults = false;
var searching = false;
var in_articles_id = false;
var override_arrow_keys = false;
function keyDown(e) {
	if (searching) {
		stopDefaults = false;
		return;
	}
	if (!e) var e = window.event;
	if (e.ctrlKey || e.altKey || e.shiftKey || e.metaKey) {
		stopDefaults = false;
		return;
	}
	var code;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if (override_arrow_keys) {
		switch(code) {
			case 38: /* up */ $('readingPane').scrollTop=$('readingPane').scrollTop-40; stopDefaults = true; return;
			case 40: /* down */ $('readingPane').scrollTop=$('readingPane').scrollTop+40; stopDefaults = true; return;
			case 37: /* left */ $('readingPane').scrollLeft=$('readingPane').scrollLeft-40; stopDefaults = true; return;
			case 39: /* right */ $('readingPane').scrollLeft=$('readingPane').scrollLeft+40; stopDefaults = true; return;
		}
	}
	switch(String.fromCharCode(code)) {
	//	case 'H':	loadArticle(1); stopDefaults = true; break;
		case 'J':	scrollDn(); stopDefaults = true; break;
		case 'K':	scrollUp(); stopDefaults = true; break;
		case ' ':	if (in_articles_id) { stopDefaults = false; break; }
		case 'O':	
		case 'V':	openCurentLink(); window.open($('currentLink').href); stopDefaults = true; break;
		case 'S':	starCurrent(); stopDefaults = true; break;
		case 'U':	keepUnreadCurrent(); stopDefaults = true; break;
		default:	stopDefaults = false; break;
	}
}
function starCurrent() {
	var id = $F('id_' + currentArtId);
	var s_action = 'add';
	if ($('starAction').className == 'actionDone') {
		s_action = 'remove';
	}
	sexyBOX('Starring and publishing.<br />Please Wait...','300');
	action('starred',s_action,id);
	return false;
}
function keepUnreadCurrent() {
	var id = $F('id_' + currentArtId);
	var u_action = 'add';
	if ($('unreadAction').className == 'actionDone') {
		u_action = 'remove';
	}
	sexyBOX('Keeping unread.<br />Please Wait...','300');
	action('unread',u_action,id);
	return false;
}
function keyPress(e) {
	if (stopDefaults) {
		e.preventDefault();
	}
}
function loadArticle(pos) {
	removeSelected();
	currentArtId = pos - 1;
	if (currentArtId >= numRows) {return;}
	var scrollBy = xTop($('art_' + currentArtId)) - xTop($('readingPane'));
	$('scrolling').style.top = ($('scrolling').offsetTop - scrollBy) + "px";
	$('articlesPane').scrollTop = 0;
	currentArtId = pos;
	loadSelected();
	return false;
}
function removeSelected() {
	if ($('chan_' + (currentArtId-1)) != null && currentArtId != 1) {
		$('chan_' + (currentArtId-1)).className = 'channel';
	}
	if (currentArtId != 0) {
		$('art_' + currentArtId).className = 'aTitle read';
		$('chan_' + currentArtId).className = 'channel';
	}
}
function loadSelected() {
	var id = $F('id_' + currentArtId);
	var articleHTML = getArticleFromCache(id);
	if (articleHTML == null) {
		new Ajax.Request('read_article.php?id=' + id, {onSuccess: parseNewArticle});
	} else {
		loadArticleHTML(articleHTML,id, true);
	}
	// Pre-fetch next article
	var nextId = $('id_' + (currentArtId+1));
	if (nextId) {
		var nextArticleHTML = getArticleFromCache(nextId.value);
		if (nextArticleHTML == null) {
			new Ajax.Request('read_article.php?pf=1&id=' + nextId.value, {onSuccess: parseNextArticle});
		}
	}
	if (currentArtId != 1) {
		$('chan_' + (currentArtId-1)).className = 'channel top';
	}
	$('art_' + currentArtId).className = 'aTitle read selected';
	$('chan_' + currentArtId).className = 'channel selected';
}
function loadArticleHTML(html,id, cached) {
	$('readingPane').innerHTML = html;
	$('readingPane').scrollTop = 0;
	if (readingMode) {
		// Might be already tagged as 'read'
		if (html.indexOf('id="unreadAction" class="actionDone"') == -1 && $('art_' + currentArtId).className != 'aTitle read') {
			action('new','remove',id);
			changeNumArticles(-1);
		}
	}
}
function parseNewArticle(t) {
	var i = t.responseText.indexOf(';');
	var id = t.responseText.substring(0,i);
	var newHtml = t.responseText.substring(i+1);
	loadArticleHTML(newHtml,id,false);
	cacheArticle(id,newHtml);
}
function parseNextArticle(t) {
	var i = t.responseText.indexOf(';');
	var id = t.responseText.substring(0,i);
	var newHtml = t.responseText.substring(i+1);
	cacheArticle(id,newHtml);
	$('prefetchPane').innerHTML = newHtml;
	$('prefetchPane').innerHTML = '';
}
function cacheArticle(id,newHtml) {
	// Cache result to not have to re-do AJAX call when re-loading this article
	articlesIdCache.push(id);
	articlesCache.push(newHtml);
	cacheSize += newHtml.length;
	// Make sure the cache has a max size; remove older elements from cache when full
	while (cacheSize > MAX_CACHE_SIZE) {
		articlesIdCache.shift();
		cacheSize -= articlesCache.shift().length;
	}
}
function getArticleFromCache(id) {
	var i=0;
	for(;i<articlesIdCache.length;i++) {
		thisId = articlesIdCache[i];
		if (thisId == id) {
			return articlesCache[i];
		}
	}
	return null;
}
function action(state,addRemove,id) {
	if (readingMode) {
		if (state == 'unread') {
			if (addRemove == 'add') {
				changeNumArticles(1);
			} else {
				changeNumArticles(-1);
			}
		}
	}
	new Ajax.Request('action.php?id=' + id + '&state=' + state + '&action=' + addRemove, {onSuccess: doneAction});

	if (state != 'new') {
		// Deprecate cache for this article
		var i=0;
		for(;i<articlesIdCache.length;i++) {
			thisId = articlesIdCache[i];
			if (thisId == id) {
				articlesCache[i] = null;
				articlesIdCache[i] = null;
				break;
			}
		}
	}
}
function doneAction(t) {
	eval(t.responseText);
	sexyHIDE();
}
function changeNumArticles(add) {
	if ($('numArticles') != null) {
		var numArticles = parseInt($('numArticles').innerHTML);
		numArticles += add;
		$('numArticles').innerHTML = numArticles;
	}
}
function grabLatest(passphrase) {
	$('grab_latest').innerHTML = 'Grabbing...';
	new Ajax.Request('load_articles.php?passphrase='+passphrase, {onSuccess: doneGrabLatest});
	return false;
}
function doneGrabLatest(t) {
	$('grab_latest').innerHTML = 'Done';
}
function grabLatestFrom(passphrase,sid) {
	$('grab_latest_'+sid).innerHTML = 'Grabbing...';
	new Ajax.Request('load_articles.php?passphrase='+passphrase+'&sid='+sid, {onSuccess: doneGrabLatestFrom});
	return false;
}
function doneGrabLatestFrom(t) {
	var response = t.responseText.split(';');
	var sid = response[0];
	$('grab_latest_'+sid).innerHTML = 'Done';
	$('last_updated_'+sid).innerHTML = response[1];
}
function confirmDelSub(id,title) {
	if (confirm("Are you sure you want to delete this subscription:\n"+ id + " - " + title + "\nClick OK to confirm or click Cancel to abort.")) {
		window.location.href = "subs.php?del=" + id;
	}
	return false;
}
function register_ff_reader() {
	window.navigator.registerContentHandler("application/vnd.mozilla.maybe.feed",gob_url+"/subs.php?new_sub_ff=%s","GobbleRSS Reader");
}
function show_feed_actions(feed_name, sub_id) {
	var f = 'Read only <em>'+feed_name+'</em> articles';
	var fo = 'Filter out <em>'+feed_name+'</em> articles (temporarily)';
	if (feed_name == '') {
		f = 'Read only articles from this feed';
		fo = 'Filter out articles from this feed (temporarily)';
	}
	new Insertion.Before('article_body', '<div class="feed_actions" id="feed_actions_block"><a href="close_feed_actions" onclick="hide_feed_actions();return false" style="float:right;right:0">X</a><a href="index.php?sub='+sub_id+'">'+f+'</a><br/><a href="index.php?not_sub='+sub_id+'">'+fo+'</a></div>');
	Element.hide('feed_actions_link');
	Element.show('feed_actions_hide_link');
}
function hide_feed_actions() {
	Element.remove('feed_actions_block');
	Element.show('feed_actions_link');
	Element.hide('feed_actions_hide_link');
}
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if (node == null) {
		node = document;
	}
	if (tag == null) {
		tag = '*';
	}
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if (pattern.test(els[i].className)) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
var maxSubTitleWidth = 0;
function resizeUrlsToMaxTitleWidth() {
	var spans = getElementsByClass('subTitle', $('allSubsTable'), 'span');
	for (i=0; i<spans.length; i++) {
		var span = spans[i];
		if (span.offsetWidth > maxSubTitleWidth) {
			maxSubTitleWidth = span.offsetWidth;
		}
	}
	var divs = getElementsByClass('subUrl', $('allSubsTable'), 'div');
	for (i=0; i<divs.length; i++) {
		var div = divs[i];
		div.style.width = maxSubTitleWidth+'px';
	}
}
function engagd_processPending(){
	new Ajax.Request('index.php?apml_engagd=pending', {onSuccess: done_engagd_processPending, on503: fail_503_engagd_processPending, onFailure: fail_engagd_processPending});
}
function fail_engagd_processPending(t){
//	alert('fail_engagd_processPending')
//	alert(t.status)
//	alert(t.statusText)
	continue_engagd_processPending('ERROR:'+t.status+' '+t.statusText);
}
function fail_503_engagd_processPending(t){
//	alert('503_engagd_processPending')
	continue_engagd_processPending('CONTINUE');
}
function done_engagd_processPending(t){
//	alert('done_engagd_processPending')
//	alert(t.responseText)
	continue_engagd_processPending(t.responseText);
}
function continue_engagd_processPending(text){
//	alert('continue_engagd_processPending('+text+')')
	if (text == 'DONE') {
		return;
	} else if (text.indexOf('ERROR') == 0) {
		alert('Error profiling pages using Engagd:\n'+text);
	} else {
		var wait = 30*1000;
		if (text.indexOf('WAIT') == 0) {
			wait = text.substring(5)*1000;
		}
//		alert('Waiting '+wait+' before continuing.');
		setTimeout('engagd_processPending()', wait);
	}
}